package com.qianwang.shopmsg.service.teacher.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qianwang.shopmsg.common.Page;
import com.qianwang.shopmsg.entity.TeacherInfo;
import com.qianwang.shopmsg.exception.ParamValidateException;
import com.qianwang.shopmsg.param.TeacherInfoCreateParam;
import com.qianwang.shopmsg.param.TeacherInfoListParam;
import com.qianwang.shopmsg.repository.TeacherMsgDao;
import com.qianwang.shopmsg.service.teacher.TeacherMsgService;
import com.qianwang.shopmsg.util.OvalUtil;

@Service("teacherMsgService")
public class TeacherMsgServiceImpl implements TeacherMsgService {

	@Resource(name="teacherMsgDao")
	private TeacherMsgDao teacherMsgDao;
	
	@Override
	public int countTeacherInfo(TeacherInfoListParam listParam) {
		Map<String,Object> param = getParam(listParam);
		
		return teacherMsgDao.countTeacherInfo(param);
	}
	
	@Override
	public void addTeacherInfo(TeacherInfo teacherInfo) {
		teacherMsgDao.addTeacherInfo(teacherInfo);
	}
	
	@Override
	public List<TeacherInfo> listTeacherInfos(Page pg,TeacherInfoListParam listParam) {
		Map<String,Object> param = getParam(listParam);
		param.put("pageSize", pg.getPageSize());  
		param.put("pageIndex", pg.getPageIndex());
		
		return teacherMsgDao.listTeacherInfos(param);
	}
	
	/**
	 * 拼接查询参数
	 * @param listParam
	 * @return
	 */
	private Map<String,Object> getParam(TeacherInfoListParam listParam){
		Map<String,Object> param = new HashMap<String, Object>();
		if(null == listParam){
			return param;
		}
		if(StringUtils.isNotBlank(listParam.getStartTime())){
			param.put("startTime", listParam.getStartTime());
		}
		if(StringUtils.isNotBlank(listParam.getEndTime())){
			param.put("endTime", listParam.getEndTime());
		}
		if(StringUtils.isNotBlank(listParam.getTeacherName())){
			param.put("teacherName", listParam.getTeacherName());
		}
		return param;
	}
	
	@Override
	@Transactional(rollbackFor=Throwable.class)
	public void saveTeacherInfo(TeacherInfoCreateParam createParam) throws ParamValidateException {
		
		OvalUtil.validate(createParam);
		
		TeacherInfo teacherInfoOld = teacherMsgDao.loadTeacherInfo(createParam.getTeacherInfoId());
		
		if(teacherInfoOld != null){
			updateTeacherInfo(createParam,teacherInfoOld);
		}else{
			createTeacherInfo(createParam);
		}
	}
	
	/**
	 * update操作
	 * @param signParam
	 * @param studentInfo 
	 */
	private void updateTeacherInfo(TeacherInfoCreateParam teacherInfoParam,TeacherInfo teacherInfoOld){
		BeanUtils.copyProperties(teacherInfoParam, teacherInfoOld);
		teacherMsgDao.updateTeacherInfo(teacherInfoOld);
	}
	
	/**
	 * create操作
	 * @param signParam
	 */
	private void createTeacherInfo(TeacherInfoCreateParam courseParam){
		TeacherInfo teacherInfo = new TeacherInfo();
		BeanUtils.copyProperties(courseParam, teacherInfo);
		
		teacherMsgDao.addTeacherInfo(teacherInfo);
	}
	
	@Override
	@Transactional(rollbackFor=Throwable.class)
	public void deleteTeacherInfo(long id) throws ParamValidateException {
		TeacherInfo teacherInfo = teacherMsgDao.loadTeacherInfo(id);
		if(null == teacherInfo){
			throw new ParamValidateException("信息不存在，请稍后重试！");
		}
		teacherMsgDao.deleteTeacherInfoById(id);
	}
	
	@Override
	public TeacherInfo loadTeacherInfoById(long id) throws ParamValidateException {
		TeacherInfo teacherInfo = teacherMsgDao.loadTeacherInfo(id);
		if(null == teacherInfo){
			throw new ParamValidateException("信息不存在，请稍后重试！");
		}
		return teacherInfo;
	}
	
}
